package cn.tedu.map;/**
 * @Author 金帮嘉诚 @Date 2021-06-23 星期三 9:43
 */
/*本类用于完成集合相关的练习*/

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * 知识点
 */
public class TestMap3 {
    public static void main(String[] args) {
        Map<Character, Integer> map = frequencyOfCharacter();
        System.out.println(map);

    }

    public static Map<Character, Integer> frequencyOfCharacter() {
        Map<Character, Integer> map = new HashMap<>();
        String string = getString();
        int length = string.length();
        for (int i = 0; i < length; i++) {
            //for循环里面的步骤：
            //1.获取将要放入的字符，作为key
            //2.获取key对应的value
            //3.判断value是否null
            //3.1null则put(key,1)
            //3.2不是null则put(key,value+1)

            Character key = string.charAt(i);
            Integer value = map.get(key);
            if (value == null) {
                map.put(key, 1);
            } else {
                map.put(key, value + 1);
            }
        }
        return map;
    }

    public static String getString() {
        System.out.println("请输入要统计的字符串");
        String string = new Scanner(System.in).nextLine();
        return string;
    }
}

